/**
 * @author Rudy Kaplan Depena (The University of Texas at Austin)
 */
package com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql;

import com.utaustin.mirankerlab.ultrawrap.compiler.components.Triple;
import java.util.List;
import java.util.ArrayList;

/** Basic Graph Pattern that contains list of triples */
public class BGP extends Operand {

	//instance variables
	private List<Triple> triples;
	
	/**
	 * Default Constructor
	 */
	public BGP(){
		super();
		triples = new ArrayList<Triple>();
	}
	
	/**
	 * Add triple to BGP's list of triples
	 * @param triple
	 */
	public void addTriple(Triple triple){ triples.add(triple); }
	
	/**
	 * Add list of triples to BGP's list of triples
	 * @param list of triples
	 */
	public void addTriples(List<Triple> triples){ this.triples.addAll(triples); }
	
	/**
	 * Add that BGP's list of triples to this BGP's list of triples
	 * @param that BGP
	 */
	public void addTriples(BGP that){ this.triples.addAll(that.getTriples()); }
	
	/**
	 * Acquire BGP's list of triples
	 * @return List of triples
	 */
	public List<Triple> getTriples(){ return triples; }
	
	/**
	 * String representation of BGP
	 * @return String
	 */
	public String toString(){
		String result = "BGP(\n";
		for(Triple triple : triples) result += triple + "\n";
		return result + ")";
	}
	
	/**
	 * Returns String representation of SQL that defines BGP
	 * @return SQL representation of BGP
	 */
	public String generateSql(boolean calledByLeftJoin){
		String sqlInnerJoins = "";
		boolean onlyOneTripleInBGP = false;
		for(int idx = 0; idx < triples.size(); ++idx){
			if(idx == 0){
				if(triples.size() == 1){onlyOneTripleInBGP = true;}
				sqlInnerJoins += triples.get(idx).sqlSelectStatement(false, calledByLeftJoin, onlyOneTripleInBGP);
			}
			else{
				if(triples.size() == 1){onlyOneTripleInBGP = true;}
				sqlInnerJoins += "\n INNER JOIN \n";
				sqlInnerJoins += triples.get(idx).sqlSelectStatement(true, calledByLeftJoin, onlyOneTripleInBGP);
			}
		}
		
		return sqlInnerJoins;
		
	}
	
}
